package com.cms.work.core.service.impl;

import java.lang.reflect.Field;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.cms.work.core.dao.BaseDao;
import com.cms.work.core.service.BaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * <p>Title: BaseService</p>
 * <p>Description: Service通用类
 * 	 继承BaseServiceImpl的业务层即可实现URCD方法
 * </p>
 * <p>Email: yuyue_qty@163.com</p>
 * @author 晴天雨
 * @date 2016年5月1日下午8:54:00
 * @version 1.0
 */
public abstract class BaseServiceImpl<T, PK> implements BaseService<T, PK> {

    @Autowired
    private BaseDao<T, PK> dao;

    @Override
    public T selectByPrimaryKey(PK key) {
        return dao.selectByPrimaryKey(key);
    }

    @Override
    public List<T> selectByCondition(T entity) {
        return dao.select(entity);
    }
    
    @Override
    public List<T> selectByCondition(Example example) {
        return dao.selectByExample(example);
    }

    @Override
    public List<T> selectAll() {
        return dao.selectAll();
    }

    @Override
    public int insert(T entity) {
        return dao.insert(entity);
    }

    @Override
    public int insertSelective(T entity) {
        return dao.insertSelective(entity);
    }

    @Override
    public int update(T entity) {
        return dao.updateByPrimaryKey(entity);
    }

    @Override
    public int updateSelective(T entity) {
        return dao.updateByPrimaryKeySelective(entity);
    }
    
    @Override
    public int deleteByPrimaryKey(PK key) {
        return dao.deleteByPrimaryKey(key);
    }

    @Override
    public PageInfo<T> selectPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<T>(selectAll());
    }

    @Override
    public PageInfo<T> selectPage(int pageNum, int pageSize, T entity) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<T>(selectByCondition(entity));
    }
    
    @Override
    public PageInfo<T> fuzzySelectPage(int pageNum, int pageSize, String orderBy, T entity) {
    	Class<? extends Object> clazz = null;
    	Example example = null;
    	Field[] fields = null;
    	try {
    		clazz = entity.getClass();
        	example = new Example(clazz);
        	fields = clazz.getDeclaredFields();
        	for (int i = 0; i < fields.length; i++) {
    			Field field = fields[i];
    			field.setAccessible(true);
    			String type = field.getType().toString();
    			if(null != field.get(entity) && !"".equals(field.get(entity))){
    				Criteria criteria = null;
    				if(type.endsWith("String")){
    					criteria = example.createCriteria();
    					criteria.andLike(field.getName(), "%"+field.get(entity)+"%");
    				}else{
    					criteria = example.createCriteria();
    					criteria.andEqualTo(field.getName(), field.get(entity));
    				}
    			}
    		}
        	PageHelper.startPage(pageNum, pageSize);
        	PageHelper.orderBy(orderBy);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return new PageInfo<T>(selectByCondition(example));
    }
    
    @Override
    public PageInfo<T> selectPage(int pageNum, int pageSize, String orderBy, T entity) {
        PageHelper.orderBy(orderBy);
        return selectPage(pageNum, pageSize, entity);
    }

    @Override
    public PageInfo<T> selectPage(PageInfo<T> pageInfo) {
        if(pageInfo == null){
            return pageInfo;
        }
        List<T> list = pageInfo.getList();
        if(list == null){
            return pageInfo;
        }
        String orderBy = pageInfo.getOrderBy();
        if(orderBy != null){
            return selectPage(pageInfo.getPageNum(), pageInfo.getPageSize(), orderBy, list.get(0));
        }else{
            return selectPage(pageInfo.getPageNum(), pageInfo.getPageSize(), list.get(0));
        }
    }

  //TODO 其他...
}
